﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit.Electric.Common;
using HYRElectric.Equipment;

namespace HYRElectric.PowerSystem
{
	public partial class DlgSignChangeSet : System.Windows.Forms.Form
	{
		public DlgSignChangeSet(Dictionary<FamilyInstance, string> dictFamilyInstanceSel, ExternalCommandData cmdDataIn, ref List<EquipmentExchangeSetInfo> lstEquipmentExchangeSetItems)
		{
			this.InitializeComponent();
			this.cmdData = cmdDataIn;
			this.doc = this.cmdData.Application.ActiveUIDocument.Document;
			this.m_dictFamilyInstanceSel = dictFamilyInstanceSel;
			this.m_dictEleMatchDbFamily = new Dictionary<FamilyInstance, EquipmentExchangeSetInfo>();
			this.m_lstEquipmentExchangeSetItems = lstEquipmentExchangeSetItems;
		}

		public Dictionary<FamilyInstance, EquipmentExchangeSetInfo> GetUserSetMatch()
		{
			return this.m_dictEleMatchDbFamily;
		}

		private void DlgSignChangeSet_Load(object sender, EventArgs e)
		{
			this.dataGridView1.RowHeadersVisible = false;
			this.dataGridView1.ColumnHeadersVisible = true;
			int num = 0;
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn.Name = "TypeOld";
			dataGridViewTextBoxColumn.HeaderText = "原设备类型";
			dataGridViewTextBoxColumn.Width = 130;
			this.dataGridView1.Columns.Insert(num++, dataGridViewTextBoxColumn);
			dataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn.Name = "NameOld";
			dataGridViewTextBoxColumn.HeaderText = "原设备名称";
			dataGridViewTextBoxColumn.Width = 130;
			this.dataGridView1.Columns.Insert(num++, dataGridViewTextBoxColumn);
			dataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn.Name = "NameNew";
			dataGridViewTextBoxColumn.HeaderText = "替换为";
			dataGridViewTextBoxColumn.Width = 130;
			this.dataGridView1.Columns.Insert(num++, dataGridViewTextBoxColumn);
			dataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn.Name = "ClassName";
			dataGridViewTextBoxColumn.HeaderText = "归属类别";
			dataGridViewTextBoxColumn.Width = 80;
			this.dataGridView1.Columns.Insert(num++, dataGridViewTextBoxColumn);
			List<string> list = new List<string>();
			this.dataGridView1.Rows.Clear();
			foreach (KeyValuePair<FamilyInstance, string> keyValuePair in this.m_dictFamilyInstanceSel)
			{
				FamilyInstance key = keyValuePair.Key;
				string value = keyValuePair.Value;
				FamilySymbol symbol = key.Symbol;
				string text = symbol.FamilyName + "/" + symbol.Name;
				if (list.IndexOf(text) < 0)
				{
					list.Add(text);
					string value2 = "请进行设置→";
					string value3 = "匹配设置";
					EquipmentExchangeSetInfo dbFamilyObj = DlgSignChangeSet.GetDbFamilyObj(symbol, this.m_lstEquipmentExchangeSetItems);
					if (dbFamilyObj != null)
					{
						value2 = Path.GetFileNameWithoutExtension(dbFamilyObj.DbFamilyObjectCur.Family);
						value3 = dbFamilyObj.EquipmentTypeName;
					}
					int index = this.dataGridView1.Rows.Add();
					this.dataGridView1.Rows[index].Cells[0].Value = value;
					this.dataGridView1.Rows[index].Cells[1].Value = text;
					this.dataGridView1.Rows[index].Cells[1].Tag = symbol;
					this.dataGridView1.Rows[index].Cells[2].Value = value2;
					this.dataGridView1.Rows[index].Cells[2].Tag = dbFamilyObj;
					DataGridViewButtonCell dataGridViewButtonCell = new DataGridViewButtonCell();
					dataGridViewButtonCell.Value = value3;
					this.dataGridView1.Rows[index].Cells[3] = dataGridViewButtonCell;
					this.dataGridView1.Rows[index].Cells[0].ReadOnly = true;
					this.dataGridView1.Rows[index].Cells[1].ReadOnly = true;
					this.dataGridView1.Rows[index].Cells[2].ReadOnly = true;
				}
			}
		}

		private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{
			if (e.ColumnIndex == 3 && e.RowIndex >= 0)
			{
				FamilySymbol familySymbol = this.dataGridView1.Rows[e.RowIndex].Cells[1].Tag as FamilySymbol;
				EquipmentExchangeSetInfo equipmentExchangeSetInfo = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Tag as EquipmentExchangeSetInfo;
				EquipmentExchangeSetInfo dbFamilyObj = DlgSignChangeSet.GetDbFamilyObj(familySymbol, this.m_lstEquipmentExchangeSetItems);
				if (dbFamilyObj != null)
				{
					this.m_selEquipType = dbFamilyObj.EquipmentType;
				}
				string text = "";
				DbFamilyObject selectFamily = new EquipmentLayoutCmdEntry(this.cmdData, this.m_selEquipType).GetSelectFamily(ref text, ref this.m_selEquipType);
				if (selectFamily == null)
				{
					return;
				}
				string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(selectFamily.Family);
				if (fileNameWithoutExtension != "")
				{
					selectFamily.Name = fileNameWithoutExtension;
				}
				bool flag = true;
				if (dbFamilyObj == null)
				{
					equipmentExchangeSetInfo = new EquipmentExchangeSetInfo();
					flag = false;
				}
				equipmentExchangeSetInfo.m_strOldEquipType = this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
				equipmentExchangeSetInfo.m_strOldEquipName = this.dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
				equipmentExchangeSetInfo.EquipmentTypeName = text;
				equipmentExchangeSetInfo.FamilySymbolCur = familySymbol;
				equipmentExchangeSetInfo.DbFamilyObjectCur = selectFamily;
				equipmentExchangeSetInfo.EquipmentType = this.m_selEquipType;
				if (!flag)
				{
					this.m_lstEquipmentExchangeSetItems.Add(equipmentExchangeSetInfo);
				}
				else
				{
					for (int i = 0; i < this.m_lstEquipmentExchangeSetItems.Count; i++)
					{
						EquipmentExchangeSetInfo equipmentExchangeSetInfo2 = this.m_lstEquipmentExchangeSetItems[i];
						if (equipmentExchangeSetInfo.OldEquipName == equipmentExchangeSetInfo2.OldEquipName && equipmentExchangeSetInfo.OldEquipType == equipmentExchangeSetInfo2.OldEquipType)
						{
							this.m_lstEquipmentExchangeSetItems[i] = equipmentExchangeSetInfo;
						}
					}
				}
				this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value = selectFamily.Name;
				this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Tag = equipmentExchangeSetInfo;
				this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = text;
			}
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
			{
				if (!(this.dataGridView1.Rows[i].Cells[2].Tag is EquipmentExchangeSetInfo))
				{
					MessageBox.Show("没有匹配完成，请继续处理");
					return;
				}
			}
			foreach (KeyValuePair<FamilyInstance, string> keyValuePair in this.m_dictFamilyInstanceSel)
			{
				FamilyInstance key = keyValuePair.Key;
				EquipmentExchangeSetInfo dbFamilyObj = DlgSignChangeSet.GetDbFamilyObj(key.Symbol, this.m_lstEquipmentExchangeSetItems);
				if (dbFamilyObj != null)
				{
					this.m_dictEleMatchDbFamily.Add(key, dbFamilyObj);
				}
			}
			base.DialogResult = DialogResult.OK;
			base.Close();
		}

		public static EquipmentExchangeSetInfo GetDbFamilyObj(FamilySymbol curFamilySymbol, List<EquipmentExchangeSetInfo> lstEquipmentExchangeSetItems)
		{
			foreach (EquipmentExchangeSetInfo equipmentExchangeSetInfo in lstEquipmentExchangeSetItems)
			{
				string text = equipmentExchangeSetInfo.OldEquipName;
				int num = text.IndexOf('/');
				if (num > 0)
				{
					text = text.Left(num);
				}
				if (curFamilySymbol.FamilyName == text)
				{
					return equipmentExchangeSetInfo;
				}
			}
			return null;
		}

		public List<EquipmentExchangeSetInfo> m_lstEquipmentExchangeSetItems;

		private Dictionary<FamilyInstance, string> m_dictFamilyInstanceSel;

		private ExternalCommandData cmdData;

		private Dictionary<FamilyInstance, EquipmentExchangeSetInfo> m_dictEleMatchDbFamily;

		private Document doc;

		private EquipmentType m_selEquipType = EquipmentType.Power;
	}
}
